<?php
namespace app\admin\controller;
use app\admin\model\Goods;
use app\admin\model\GoodsOrder;
use app\admin\model\GoodsType;
use think\Request;

class DiyGoods extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }
    public function index(){
        return $this->fetch('public/help');
    }

    public function goodsList(){
        $goods = new Goods();
        $where['id'] = ['neq',0];
        $rs = $goods->getPageAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function addGoods(Request $request){
        if($request->post()){
            $data = $request->post();
            $goods = new Goods();
            $rs = $goods->add($data);
            if($rs){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        $brand = new \app\admin\model\Brand();
        $where['id'] = ['neq',0];
        $bList = $brand->getAll($where);

        $good_type = new GoodsType();
        $where['id'] = ['neq',0];
        $typeList = $good_type->getAll($where);

        $this->assign('bList',$bList);
        $this->assign('typeList',$typeList);
        return $this->fetch();
    }

    public function goodsTypeList(){
        $goodsType = new GoodsType();
        $where['id'] = ['neq',0];
        $rs = $goodsType->getPageAll($where);

        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function addGoodsType(Request $request){
        if($request->post()){
            $data = $request->post();
            $goodsType = new GoodsType();
            if($data["name"] == '' || $data["sort"] == '' || $data["status"] == '')return $this->error('参数不完整');
            $rs = $goodsType->add($data);
            if($rs){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function editGoodsType(Request $request){
        $goodsType = new GoodsType();
        if($request->post()){
            $data = $request->post();
            $where['id']  = $data['id'];
            unset($data['id']);
            $rs = $goodsType->edit($where,$data);
            if($rs){
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }else{
            $where['id'] = $request->get('id');
            $rs = $goodsType->getOne($where);
            $this->assign('rs',$rs);
        }
        return $this->fetch();
    }

    public function goodsOrderList(){
        $goodsOrder = new GoodsOrder();
        $where['create_time'] = ['neq',0];
        $rs = $goodsOrder->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function editGoodsOrder(Request $request){
        $goodsOrder = new GoodsOrder();
        if($request->post()){
            $data = $request->post();
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $goodsOrder->edit($where,$data);
            if($rs){
                return $this->success('修改成功');
            }
            return $this->error('修改失败');
        }else{
            $data = $request->get();
            $where['id']  = $data['id'];
        }
        $rs = $goodsOrder->getOne($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }
}